package com.infore.airsupervise.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import com.beardedhen.androidbootstrap.AwesomeTextView;
import com.beardedhen.androidbootstrap.font.FontAwesome;
import com.infore.airsupervise.R;

import com.infore.airsupervise.adapter.MainFragAdapter;

import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.ui.fragment.AutoEventFragment;

import com.infore.airsupervise.ui.fragment.ManualOperationFragment;
import com.infore.airsupervise.ui.widget.NoScrollViewPager;
import com.infore.airsupervise.utils.DialogUtil;
import com.infore.airsupervise.utils.JumpUtil;
import com.rey.material.app.DialogFragment;
import com.rey.material.app.SimpleDialog;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 事件记录列表界面
 * Created by xiaotao on 2017/8/15.
 */

public class EventsListActivity extends BaseActivity implements  CompoundButton.OnCheckedChangeListener {


    @BindView(R.id.event_title_left_menu)
    AwesomeTextView eventTitleLeftMenu;

    @BindView(R.id.rb_autorecord)
    RadioButton rbAutorecord;
    @BindView(R.id.rb_manual)
    RadioButton rbManual;

    @BindView(R.id.event_title_right_menu)
    AwesomeTextView eventTitleRightMenu;
    @BindView(R.id.event_viewpager)
    NoScrollViewPager mViewpager;



    private String[] dataTypeArr = new String[]{"全部","未处理", "处理中", "已完成"};
    private int dataType = 0; // 记录数据类型
    private OnDataTypeChangeListener listener,mlistener;
    private int tabindex=-1;

    private boolean isCreateFromAuto; //是否有从自动 界面创建的新的事件 当viepager切换到手动界面是刷新界面的标识

    public boolean isCreateFromAuto() {
        return isCreateFromAuto;
    }

    public void setCreateFromAuto(boolean createFromAuto) {
        isCreateFromAuto = createFromAuto;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_events_list);
        ButterKnife.bind(this);
        initView();
        initViewPager();

    }

    private void initView() {
        eventTitleLeftMenu.setFontAwesomeIcon(FontAwesome.FA_CHEVRON_LEFT);
        eventTitleRightMenu.setFontAwesomeIcon(FontAwesome.FA_FILTER);
        LinearLayout mSearchShow = (LinearLayout)findViewById(R.id.event_search_show);
        mSearchShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JumpUtil.jumpFromTo(EventsListActivity.this, SearchDataRateActivity.class, Constants.INTENT_INT_KEY ,tabindex);
                overridePendingTransition(R.anim.slide_up_in,
                        R.anim.slide_up_out);
            }
        });

        rbAutorecord.setOnCheckedChangeListener(this);
        rbManual.setOnCheckedChangeListener(this);
        rbAutorecord.setChecked(true);
    }
    private void initViewPager() {
        List<Fragment> fragments = new ArrayList<>();
        AutoEventFragment waterQualityFragment = new AutoEventFragment();
        ManualOperationFragment operationFragment=new ManualOperationFragment();
        fragments.add(waterQualityFragment);
        fragments.add(operationFragment);
        MainFragAdapter fragmentAdapter = new MainFragAdapter(getSupportFragmentManager(), fragments, null);
        mViewpager.setAdapter(fragmentAdapter);
        listener=waterQualityFragment;
        mlistener=operationFragment;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        if (!isChecked) {
            return;
        }

        switch (buttonView.getId()) {
            case (R.id.rb_autorecord):
                tabindex=0;
                setViewPagerCurrentItem(0);
                break;
            case (R.id.rb_manual):
                tabindex=1;
                setViewPagerCurrentItem(1);
                break;
        }

    }

    private void setViewPagerCurrentItem(int index) {

        mViewpager.setCurrentItem(index,false);
    }

    /**
     * 条件筛选点击
     */
    @OnClick(R.id.event_title_right_menu)
    void onRightBtnClick() {
        DialogUtil.getInstance().radioChoice("条件筛选", dataTypeArr, dataType,
                new DialogUtil.OnDialogActionListener(){

                    @Override
                    public void onPositiveActionClicked(DialogFragment fragment) {
                        SimpleDialog simpleDialog = (SimpleDialog) fragment.getDialog();

                        dataType = simpleDialog.getSelectedIndex();
                        Log.e("dataType",dataType+"");
                        // 通知Fragment刷新
                        if(tabindex==0){
                            listener.onDataTypeChange(dataType);
                        }else{
                            mlistener.onDataTypeChange(dataType);
                        }

                    }

                    @Override
                    public void onNegativeActionClicked(DialogFragment fragment) {}
                })
                .show(getSupportFragmentManager(), "tag");
    }


    /**
     * 返回上个界面
     */
    @OnClick(R.id.event_title_left_menu)
    void leftMenuClick() {
     finish();
    }

    /**
     * 数据类型切换监听接口类
     */
    public interface OnDataTypeChangeListener {
        /**
         * 数据类型切换
         * @param dataType 0：全部，1：未完成，2：处理中，3：已完成
         */
        void onDataTypeChange(int dataType);
    }

    public OnDataTypeChangeListener getListener() {
        return listener;
    }

    public void setListener(OnDataTypeChangeListener listener) {
        this.listener = listener;
    }
}
